package com.jiaquyun.jcps.mode.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.jiaquyun.jcps.entity.*
import com.jiaquyun.jcps.mode.BaseManager
import com.jiaquyun.jcps.mode.BaseViewModel
import com.jiaquyun.jcps.mode.manager.MineManager
import com.lemlin.network.HttpServiceData
import com.lemlin.network.HttpServiceResponse
import com.lemlin.network.ViewModelSubscribe
import io.reactivex.disposables.Disposable

class MineViewModel(application: Application) : BaseViewModel(application) {


    private val mineManager: MineManager = MineManager()

    val personalLiveData = MutableLiveData<HttpServiceResponse<PersonalResponse>>()

    val todayLiveData = MutableLiveData<HttpServiceResponse<TodayResponse>>()

    val userInfoLiveData = MutableLiveData<HttpServiceResponse<UserInfo2Response>>()
    val baseSettleLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()
    val settleDataLiveData = MutableLiveData<HttpServiceResponse<SettleResponse>>()

    /**
     * 个人页数据
     * */
    fun personalData() {
        val listener = object : ViewModelSubscribe<PersonalResponse>(personalLiveData) {}
        val disposable: Disposable = mineManager.personalData(listener)
        dispose.add(disposable)
    }

    /**
     * 今日数据
     * */
    fun todayData() {
        val listener = object : ViewModelSubscribe<TodayResponse>(todayLiveData) {}
        val disposable: Disposable = mineManager.todayData(listener)
        dispose.add(disposable)
    }

    /**
     * 个人信息
     * */
    fun userInfo() {
        val listener = object : ViewModelSubscribe<UserInfo2Response>(userInfoLiveData) {}
        val disposable: Disposable = mineManager.userInfo(listener)
        dispose.add(disposable)
    }


    /**
     * 结算
     * */
    fun cashAdd() {
        val listener = object : ViewModelSubscribe<HttpServiceData>(baseSettleLiveData) {}
        val disposable: Disposable = mineManager.cashAdd(listener)
        dispose.add(disposable)
    }


    private var page: Int = 1
    private var limit: Int = 30

    /**
     * 结算记录
     * */
    fun cashLog(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<SettleResponse>(settleDataLiveData) {
            override fun onSuccess(resp: SettleResponse) {
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            mineManager.cashLog(page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


    val signClockLiveData = MutableLiveData<HttpServiceResponse<SignClockResponse>>()

    /**
     * 考勤打卡数据
     * */
    fun punchTheClock() {
        val listener = object : ViewModelSubscribe<SignClockResponse>(signClockLiveData) {}
        val disposable: Disposable = mineManager.punchTheClock(listener)
        dispose.add(disposable)
    }


    val goToWorkLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()

    /**
     * 考勤打卡数据
     * */
    fun goToWork() {
        val listener = object : ViewModelSubscribe<HttpServiceData>(goToWorkLiveData) {}
        val disposable: Disposable = mineManager.goToWork(listener)
        dispose.add(disposable)
    }


    val goOffWorkLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()

    /**
     * 考勤打卡数据
     * */
    fun goOffWork() {
        val listener = object : ViewModelSubscribe<HttpServiceData>(goOffWorkLiveData) {}
        val disposable: Disposable = mineManager.goOffWork(listener)
        dispose.add(disposable)
    }
}